package com.lipuwater.agent.ui.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.lipuwater.agent.R;
import com.lipuwater.agent.ui.popwindown.SearchFilterPop;

import java.util.List;

/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2017/4/12.
 * Email:924686754@qq.com
 * 搜索过滤
 */
public class SearchFilterUtils implements SearchFilterPop.ChooseCallback {
    private Activity mActivity;
    private View mContentView;

    private EditText editSearch;
    private TextView textHint;
    private TextView textCancel;
    private TextView textFilter;
    private View viewBack;

    private SearchCallback mSearchCallback;

    private String mFilterType = "综合查询";
    private String mSearchContent = "";
    private List<String> mFilterList;

    public SearchFilterUtils(Activity activity, View contentView, List<String> filterList, SearchCallback searchCallback) {
        this.mActivity = activity;
        this.mContentView = contentView;
        this.mFilterList = filterList;
        this.mSearchCallback = searchCallback;
        if (mActivity == null || mContentView == null || mFilterList == null) {
            return;
        }
        initView();
    }

    private void initView() {
        editSearch = (EditText) mContentView.findViewById(R.id.edit_search);
        textHint = (TextView) mContentView.findViewById(R.id.text_hint);
        textCancel = (TextView) mContentView.findViewById(R.id.text_cancel);
        textFilter = (TextView) mContentView.findViewById(R.id.text_filter);
        viewBack = mContentView.findViewById(R.id.view_back);
        final SearchFilterPop searchFilterPop = new SearchFilterPop(mActivity, textFilter, viewBack, mFilterList, this);
        /**
         * 取消或者搜索点击事件
         */
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchFilterPop != null && searchFilterPop.isShowing()) {
                    searchFilterPop.dismiss();
                }
                if (textCancel.getText().toString().equals("搜索") && mSearchCallback != null) {
                    mSearchCallback.results(mFilterType, editSearch.getText().toString());
                }
                mSearchContent = editSearch.getText().toString();
                editSearch.setFocusable(false);
            }
        });
        /**
         * 条件筛选点击事件
         */
        textFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchFilterPop != null && !searchFilterPop.isShowing()) {
                    searchFilterPop.show();
                } else if (searchFilterPop != null) {
                    searchFilterPop.dismiss();
                }
            }
        });
        /**
         * 输入框触摸事件
         */
        editSearch.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (searchFilterPop != null && searchFilterPop.isShowing()) {
                    searchFilterPop.dismiss();
                }
                editSearch.setFocusable(true);
                editSearch.setFocusableInTouchMode(true);
                editSearch.requestFocus();
                return false;
            }
        });
        /**
         * 点击软键盘的搜索响应事件
         */
        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 当按了搜索之后关闭软键盘
                    if (textCancel.getText().toString().equals("搜索") && mSearchCallback != null) {
                        mSearchCallback.results(mFilterType, editSearch.getText().toString());
                    }
                    mSearchContent = editSearch.getText().toString();
                    editSearch.setFocusable(false);
                    return true;
                }
                return false;
            }
        });
        /**
         * 监听输入框是否获取焦点的事件
         */
        editSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                //如果输入法在窗口上已经显示，则隐藏，反之则显示
                final View view = mActivity.getWindow().peekDecorView();
                InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (hasFocus) {
                    textCancel.setVisibility(View.VISIBLE);
                    textFilter.setVisibility(View.VISIBLE);
                    textHint.setVisibility(View.GONE);
                    editSearch.setHint("搜索");
                    editSearch.setText(mSearchContent);
                    editSearch.setHintTextColor(Color.parseColor("#dbdbdb"));
                    if (searchFilterPop != null && searchFilterPop.isShowing()) {
                        searchFilterPop.dismiss();
                    }
                    imm.showSoftInput(editSearch, InputMethodManager.SHOW_FORCED);
                } else {
                    textCancel.setVisibility(View.GONE);
                    textFilter.setVisibility(View.GONE);
                    textHint.setVisibility(View.VISIBLE);
                    editSearch.setHint("");
                    editSearch.setText("");
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
            }
        });
        /**
         * 监听输入框内容变化的事件
         */
        editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    textCancel.setText("取消");
                } else {
                    textCancel.setText("搜索");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    public void results(int position, String string) {
        mFilterType = string;
        textFilter.setText(string);
    }

    public interface SearchCallback {
        void results(String filterType, String searchContent);
    }

    public void closeSearch() {
        if (!editSearch.isFocusable()) {
            return;
        }
        editSearch.setFocusable(false);
    }
}
